package com.kgc.sso.listener;

import com.kgc.dao.TbItemMapper;
import com.kgc.entity.TbUser;
import com.kgc.sso.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

//监控mq ,从mq中得到新增加商品编号  itemId
//将新增加的商品增加solr库中
public class ActiveMqMessageListener implements MessageListener {

@Autowired
private UserService userService;

@Autowired
private MailSender  mailSender;
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage=(TextMessage)message;
        String code=null;
        try {
            Thread.sleep(1000);
           code=textMessage.getText();
           //发一个邮件
            TbUser user=userService.getUserByCode(code);
            sendEmail(user.getEmail(),code);
            //1.根据code得到user
            //2.链接http://localhost:8084/user/code
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void sendEmail(String email,String code){
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();

        simpleMailMessage.setTo(email);
        simpleMailMessage.setFrom("2946036985@qq.com");
        simpleMailMessage.setCc("2946036985@qq.com");
        simpleMailMessage.setSubject("测试邮件");
        String text="请激活,<a href=http://localhost:8084/user/jihuo/"+code+">激活</a>";
        simpleMailMessage.setText(text);
        mailSender.send(simpleMailMessage);
    }


    private void sendSms(){}

}
